<?php

/* 样板 Sample */
class SampleModel extends BaseModel
{
    var $table  = 'sample';
    var $prikey = 'sample_id';
    var $_name  = 'sample';
	
    var $_relation = array(
		'has_sourcing' => array(
			'model' => 'sourcing',
			'type' => HAS_ONE,
            'foreign_key'   => 'key_id',
            'dependent'     => true
		),
    	'has_sourcing_info' => array(
    		'model' => 'sourcing_info',
    		'type' => HAS_ONE,
    		'foreign_key'   => 'sample_id',
    		'dependent'     => true
    	),
		'belongs_brand' => array(
			'model' => 'brand',
			'type' => BELONGS_TO,
			'foreign_key' => 'brand_id',
            'reverse'       => 'has_sample',
		),
// 		'belongs_buyer' => array(
// 			'model' => 'member',
// 			'type' => BELONGS_TO,
// 			'foreign_key' => 'buyer_id',
//             'reverse'       => 'has_sample',
// 		),
		'belongs_merchandiser' => array(
			'model' => 'member',
			'type' => BELONGS_TO,
			'foreign_key' => 'merchandiser_id',
            'reverse'       => 'has_sample',
		),
		'belongs_style' => array(
			'model' => 'style',
			'type' => BELONGS_TO,
			'foreign_key' => 'style_id',
            'reverse'       => 'has_sample',
		),
		'belongs_fabric' => array(
			'model' => 'fabric',
			'type' => BELONGS_TO,
			'foreign_key' => 'fabric_id',
            'reverse'       => 'has_sample',
		),
		'belongs_feature' => array(
			'model' => 'feature',
			'type' => BELONGS_TO,
			'foreign_key' => 'feature_id',
            'reverse'       => 'has_sample',
		),
		'belongs_special' => array(
			'model' => 'special',
			'type' => BELONGS_TO,
			'foreign_key' => 'special_id',
            'reverse'       => 'has_sample',
		),
		'belongs_factory' => array(
			'model' => 'factory_info',
			'type' => BELONGS_TO,
			'foreign_key' => 'factory_id',
            'reverse'       => 'has_sample',
		),
  		'has_sourcing' => array(
			'model' => 'Pro_records',
			'type' => HAS_MANY,
            'foreign_key'   => 'pro_id',
            'dependent'     => true
		),
		'has_fabric_info' => array(
			'model' => 'fabric_info',
			'type' => HAS_ONE,
			'foreign_key' => 'sample_id',
            'reverse'       => 'belongs_sample',
		),
    	'has_quote' => array(
    		'model'         => 'quote',
    		'type'          => HAS_MANY,
    		'foreign_key'   => 'sample_id',
    		'dependent'     => true
    	),
    	'has_evaluate' => array(
    		'model' => 'evaluate',
    		'type' => HAS_ONE,
    		'foreign_key' => 'sample_id',
    		'dependent'     => true
    	)
	);
	
    function get_client_sample_list($conditions,$order,$limit){
    	$member_mod    =& m('member');
    	$brand_mod =& m('brand');
    	
    	$fields = "sample.sample_id,sample.sample_sn,sample.ref_sample_sn,brand.brand_name,member.user_name,mem.user_name as member_name,sample.factory_price,sample.order_sn,sample.status,sample.add_time,sample.send_sample_date,sample.front_pic,sample.front_pic_small,consumer_price";
    	$tables = "{$this->table} sample " .
    	"LEFT JOIN {$brand_mod->table} brand ON brand.brand_id = sample.brand_id " .
    	"LEFT JOIN {$member_mod->table} member ON member.user_id = sample.merchandiser_id " .
    	"LEFT JOIN {$member_mod->table} mem ON mem.user_id = sample.buyer_id ";
    	
    	/* 条件(WHERE) */
    	$conditions = $this->_getConditions($conditions, true);
    	/* 排序(ORDER BY) */
    	if ($order)
    	{
    		$order = ' ORDER BY ' . $this->getRealFields($order);
    	}
    	else
    	{
    		' ORDER BY add_time desc ';
    	}
    	
    	/* 分页(LIMIT) */
    	$limit && $limit = ' LIMIT ' . $limit;
    	if ($limit)
    	{
    		$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
    	}
    	
    	/* 完整的SQL */
    	$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
    	
    	$sample_list =$this->db->getAll($sql);
    	return $sample_list;
    }
    
    function get_all_sample_list($conditions,$order,$limit){
    	$member_mod    =& m('member');
    	$brand_mod =& m('brand');
    	$style_mod = & m('style');
    	$factory_mod = & m('factory_info');
    	$special_mod = & m('special');
    	 
    	$fields = "sample.*,brand.*,member.user_name,mem.user_name as member_name,style.*,factory.*,special.*";
    	$tables = "{$this->table} sample " .
    	"LEFT JOIN {$brand_mod->table} brand ON brand.brand_id = sample.brand_id " .
    	"LEFT JOIN {$member_mod->table} member ON member.user_id = sample.merchandiser_id " .
    	"LEFT JOIN {$style_mod->table} style ON style.style_id=sample.style_id " .
    	"LEFT JOIN {$factory_mod->table} factory ON factory.factory_id=sample.factory_id " .
    	"LEFT JOIN {$special_mod->table} special ON special.special_id=sample.special_id " .
    	"LEFT JOIN {$member_mod->table} mem ON mem.user_id = sample.buyer_id ";
    	 
    	/* 条件(WHERE) */
    	$conditions = $this->_getConditions($conditions, true);
    	/* 排序(ORDER BY) */
    	if ($order)
    	{
    		$order = ' ORDER BY ' . $this->getRealFields($order);
    	}
    	 
    	/* 分页(LIMIT) */
    	$limit && $limit = ' LIMIT ' . $limit;
    	if ($limit)
    	{
    		$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
    	}
    	 
    	/* 完整的SQL */
    	$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
    	 
    	$sample_list =$this->db->getAll($sql);
    	return $sample_list;
    }
}

?>